﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using Huirui.Cavan.Application.MainBoundedContext.SalesModule;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.SalesModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.SalesModule;
using Newtonsoft.Json;
using System.Linq;
using Huirui.Cavan.Application.MainBoundedContext.StockModule;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;

namespace Huirui.Cavan.Presentation.Controller.SalesModule
{
    #region SalesReturn

    public class SalesReturnController : EntityController<SalesReturn, string, SalesReturnSearch, ISalesReturnViewModel, ISalesReturnAppService>, ISalesReturnController
    {
        private IWareHouseAppService WareHouseAppService { get; set; }
        private IEmployeeAppService EmployeeAppService { get; set; }
        private ICustomerAppService CustomerAppService { get; set; }

        public override string ControllerName
        {
            get { return ControllerConst.SALESRETURN; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public SalesReturnController()
        {
            this.Model = ModelFactory.CreateModel<ISalesReturnViewModel>();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "salesreturn_query")]
        public ActionResult AddSalesReturn()
        {
            return View("~/Views/Sales/v1/addSalesReturn.cshtml");
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "salesreturn_edit")]
        public JsonResult AddSalesReturn(SalesReturnViewModel model)
        {
            BoolResult jsonResult = null;
            model.Item.StockInOut = new StockInOut();
            IList<SalesJson> sales = JsonConvert.DeserializeObject<IList<SalesJson>>(model.Sales);
            model.Item.StockInOut.StockInOutDetails = JsonConvert.DeserializeObject<IList<StockInOutDetail>>(model.StockInOutDetails);
            model.Item.Customer = this.CustomerAppService.GetById(model.Item.Customer.Id);
            //model.Item.Project = this.ProjectAppService.GetById(model.Item.Project.Id);
            model.Item.SaleEmployee = this.EmployeeAppService.GetById(model.Item.SaleEmployee.Id);
            model.Item.WareHouse = this.WareHouseAppService.GetById(model.Item.WareHouse.Id);
            foreach (SalesJson s in sales)
            {
                foreach (StockInOutDetail siod in model.Item.StockInOut.StockInOutDetails.Where(si => si.Description == s.Id))
                {
                    siod.WareHouse = model.Item.WareHouse;
                }
                BoolGenericResult<SalesReturn> result = this.AppService.AddOrUpdate(model.Item);
                jsonResult = new BoolResult(result.IsSuccess, result.Messages);
            }

            return Json(jsonResult, "application/json");
        }
    }
    #endregion
}